const util = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollHeight: '',
    currtab: 0,
    refreshData: {
      page: 1,
      size: 10,
      isEnd: false
    }, //分页信息
    canCommit: false,
    dataCoupon: []
  },
  /**
   * 计算滚动的可用高度
   */
  computeScrollViewHeight() {
    let that = this;
    let query = wx.createSelectorQuery().in(this);
    query.select('.coupon-header').boundingClientRect();
    query.exec(res => {
      let searchHeight = res[0].height;
      //let balanceHeight = res[1].height;
      let windowHeight = wx.getSystemInfoSync().windowHeight;
      let scrollHeight = windowHeight - searchHeight;
      // let scrollHeightSon = windowHeight - searchHeight - balanceHeight;
      this.setData({
        scrollHeight: scrollHeight,
        //scrollHeightSon: scrollHeightSon
      });
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //请求分享数据
    wx.request({
      url: app.globalData.ApiUrl + '/index.php?act=wx_share&op=get_share',
      data: {},
      method: 'GET',
      dataType: 'json',
      success: res => {
        if (res.data.code == '200') {
          this.setData({
            share_default_title: res.data.datas.title,
            share_default_image_url: res.data.datas.imageUrl
          })
        }
      }
    })
    util.share();
    var self = this;
    wx.getStorage({
      key: 'key',
      success: (res) => {
        self.setData({
          member_key: res.data
        });
        self.getData();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.computeScrollViewHeight();
    //初始化图形验证码
    this.check = this.selectComponent("#check");
    this.check.drawPic();
  },
  /**
   * tab点击事件
   */
  tabClick: function (event) {
    var self = this;
    var tab = event.currentTarget.dataset.tab;
    self.setData({
      currtab: tab,
    });
  },
  /**
   * 获取数据
   */
  getData: function () {
    var self = this;
    if (self.data.refreshData.isEnd) {
      return;
    }
    wx.showLoading({
      title: "加载中",
    })
    //获取列表数据
    wx.request({
      url: app.globalData.ApiUrl + '/index.php?act=member_voucher&op=voucher_list&key=' + self.data.member_key + '&curpage=' + self.data.refreshData.page + '&page=' + self.data.refreshData.size,
      success(res) {
        let tempData = [];
        //如果是第一页数据只添加新的数据 
        self.data.refreshData.page > 1 ? tempData = self.data.dataCoupon.concat(res.data.datas.voucher_list) : tempData = res.data.datas.voucher_list;
        self.setData({
          dataCoupon: tempData, //获取数据数组   
        });
        if (!res.data.hasmore) {
          self.data.refreshData.isEnd = true; //把“没有数据”设为true，显示  
          self.setData({
            refreshData: self.data.refreshData
          });
        }
      },
      fail: function () {
        wx.showToast({
          title: '数据加载失败',
          icon: 'none'
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  /*
   *监听充值卡的输入
   */
  cardInput: function (event) {
    var self = this;
    if (event.detail.value.length > 0) {
      self.data.canCommit = true;
    } else {
      self.data.canCommit = false;
    }
    self.setData({
      canCommit: self.data.canCommit
    });
  },
  /**
   * 卡片提交
   */
  commitCard: function (event) {
    var self = this;
    if (self.data.canCommit) {
      self.check.drawPic();
      self.check.showDialog(event.detail);
    }
  },
  //图像验证码确认事件
  _vertifySuccess(detail) {
    this.rechageSubmit(detail.detail.value.cardn);
  },
  //禁止swiper左右切换
  stopTouchMove: function () {
    return false;
  },
  //充值提交
  rechageSubmit: function (cardN) {
    wx.showLoading({
      title: "提交中",
    })
    var self = this;
    wx.request({
      url: app.globalData.ApiUrl + '/index.php?act=member_voucher&op=voucher_pwex',
      method: "POST",
      data: {
        key: self.data.member_key,
        pwd_code: cardN,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data.code == 200) {
          self.setData({
            currtab: 0
          });
          self.data.refreshData.page = 1;
          self.data.refreshData.isEnd = false;
          self.getData();
          wx.showToast({
            title: '充值成功',
            icon: 'success'
          })
        } else {
          wx.showToast({
            title: res.data.datas.error,
            icon: 'none'
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        })
      },
      complete: function (res) {
        //wx.hideLoading();
      }
    })
  },
  //滚动到底部触发事件  
  searchScrollLower: function () {
    let that = this;
    if (!that.data.isEnd) {
      that.data.refreshData.page = that.data.refreshData.page + 1;
      that.getData();
    }
  },
  onShareAppMessage: function () {
    let share_key = this.data.member_key ? this.data.member_key : '';
    let share_path = '';
    if (util.getCurrentPageUrlWithArgs().indexOf("?") != -1) {
      share_path = util.getCurrentPageUrlWithArgs() + '&share_key=' + share_key;
    } else {
      share_path = util.getCurrentPageUrlWithArgs() + '?share_key=' + share_key;
    }
    return {
      title: this.data.share_default_title,
      imageUrl: this.data.share_default_image_url,
      path: share_path
    }
  }
})